package servlet;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.Array;
import java.util.Arrays;
import java.util.Map;

public class ServletDemo extends HttpServlet {
    private static final long serialVersionUID = 1l;
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//        请求参数中文乱码
        req.setCharacterEncoding("UTF-8");

//        使用参数名获得一个值
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        System.out.println(username +"  "+ password);

        String[] hobbies = req.getParameterValues("hobby");
        System.out.println(Arrays.toString(hobbies));

        System.out.println("----------------");

        Map<String,String[]> allData = req.getParameterMap();
        for (Map.Entry<String,String[]> entry : allData.entrySet()){
            System.out.println(entry.getKey());
            System.out.println("-->");
            System.out.println(Arrays.toString(entry.getValue()));
        }

        System.out.println("get请求响应");
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req,resp);
    }
}
